package cn.lyh.FoodSystem.Service.ImpI;

import cn.lyh.FoodSystem.Dao.StatusDao;
import cn.lyh.FoodSystem.Dto.Result;
import cn.lyh.FoodSystem.Entity.Status;
import cn.lyh.FoodSystem.Service.StatusService;
import cn.lyh.edu.domain.EduLesson;
import cn.lyh.edu.domain.EduLessonType;
import cn.lyh.edu.repository.EduLessonRepository;
import cn.lyh.edu.repository.EduLessonTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import static cn.lyh.FoodSystem.Tool.PinyinUtil.getFirstLetter;

@Service
@Transactional(readOnly = true)
public class StatusServiceImpl implements StatusService {
    private final StatusDao statusDao;

    public StatusServiceImpl(StatusDao statusDao) {
        this.statusDao = statusDao;
    }

    @Autowired
    private EduLessonRepository lessonDao;

    @Autowired
    private EduLessonTypeRepository lessonTypeDao;

    /**
     * 新增字典状态
     * @param status
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED )
    public Result addTabStatus(Status status){
        Result result = new Result();
        try {
            if(status.getName()!=null&& status.getType()!=null){
                if (status.getCode()==null){
                    Random random = new Random();
                    int randomNumber = random.nextInt(90) + 10;
                    status.setCode(getFirstLetter(status.getName()+randomNumber));
                }
                status.setStatus("1");
                Status status1 = statusDao.save(status);
                if (status1.getId()!=null){
                    result.setCode(200);
                    result.setMsg("状态添加成功！");
                }else{
                    result.setCode(400);
                    result.setMsg("添加失败！");
                }
            }else {
                result.setCode(400);
                result.setMsg("参数不能为空！");
            }
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("程序错误！");
            result.setData(e);
        }
        return result;
    }
    /**
     * 查询字典状态
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED )
    public Result queryStatus(Status status){
        Result result = new Result();
        try {
                List<Status> activeStatuses = statusDao.findByType(status.getType());
                result.setCode(200);
                result.setData(activeStatuses);
                result.setMsg("状态查询成功！");
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("程序错误！");
            result.setData(e);
        }
        return result;
    }


    /**
     * 查询课程字典
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED )
    public Result queryLesson(){
        Result result = new Result();
        try {
            List<EduLesson> lessonList = lessonDao.findAll();
            List<Status> statusList =new ArrayList<>();
            for (EduLesson item:lessonList){
                Status status = new Status();
                status.setId(Math.toIntExact(item.getId()));
                status.setName(item.getLessonName());
                status.setCode(String.valueOf(item.getId()));
                List<EduLessonType> lessonTypeList =lessonTypeDao.findAllById(Collections.singleton(item.getLessonTypeId()));
                status.setType(lessonTypeList.get(0).getCode());
                statusList.add(status);
            }
            result.setCode(200);
            result.setData(statusList);
            result.setMsg("课程字典查询成功！");
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("程序错误！");
            result.setData(e);
        }
        return result;
    }




}
